const express = require('express')
const router = express.Router()
const database = require('../db/users')
const jwt = require('jsonwebtoken')

//账号登录
router.post('/api/login', (req, res) => {
  let {
    userTel,
    userPwd
  } = req.body
  //查询手机号
  database.find({
    tel: userTel
  }).then(data => {
    //手机号不存在
    if (!data.length) {
      return res.send({
        code: 301,
        success: false,
        msg: '手机号不存在'
      })
    } else {
      //手机号存在,验证密码是否正确
      if (data[0].pwd === userPwd) {
        //密码正确,更新token,返回用户信息
        let token = createToken(userTel)
        database.updateMany({tel:userTel},{token}).then(()=>{
          data[0].token = token
          return res.send({
            code: 200,
            success: true,
            msg: '登录成功',
            data: data[0]
          })
        })
      } else {
        //密码错误
        return res.send({
          code: 302,
          success: false,
          msg: '密码错误'
        })
      }
    }
  })
})
//验证码登录
router.post('/api/addUser', (req, res) => {
  let {
    phone
  } = req.body
  //查询手机号
  database.find({
    tel: phone
  }).then(data => {
    let token = createToken(phone)
    //手机号不存在新增一个用户
    if (!data.length) {
      let nickName = `茶友${Math.floor(Math.random() * 9000 + 1000)}`
      database.create({
        tel: phone,
        imgUrl: '/images/user.jpeg',
        nickName,
        token
      })
      return res.send({
        code: 200,
        success: true,
        msg: '登陆成功',
        data: {
          imgUrl: '/images/user.jpeg',
          phone,
          nickName,
          token
        }
      })
    } else {
      //手机号存在,更新token,返回个人信息
      database.updateMany({tel:phone},{token}).then(()=>{
        data[0].token = token
        return res.send({
          code: 200,
          success: true,
          msg: '登录成功',
          data: data[0]
        })
      })
    }
  })
})
//注册
router.post('/api/register', (req, res) => {
  let {
    phone,
    userPwd
  } = req.body
  //查询手机号
  database.find({
    tel: phone
  }).then(data => {
    //手机号不存在新增一个用户，并返回个人信息
    let token = createToken(phone)
    if (!data.length) {
      let nickName = `茶友${Math.floor(Math.random() * 9000 + 1000)}`
      database.create({
        tel: phone,
        pwd: userPwd,
        nickName,
        imgUrl: '/images/user.jpeg',
        token
      })
      return res.send({
        code: 200,
        success: true,
        msg: '注册成功',
        data: {
          imgUrl: '/images/user.jpeg',
          phone,
          nickName,
          token
        }
      })
    } else {
      //手机号存在,注册失败
      return res.send({
        code: 400,
        success: false,
        msg: '用户已经存在'
      })
    }
  })
})
//查询手机号是否存在
router.post('/api/selectUser', (req, res) => {
  let {
    phone
  } = req.body
  //查询手机号
  database.find({
    tel: phone
  }).then(data => {
    //手机号不存在直接返回
    if (!data.length) {
      return res.send({
        code: 301,
        success: false,
        msg: '手机号未注册'
      })
    } else {
      //存在
      return res.send({
        code: 200,
        success: true
      })
    }
  })
})
//修改密码
router.post('/api/recovery', (req, res) => {
  let {
    phone,
    pwd
  } = req.body
  //查询手机号
  database.updateMany({
    tel: phone
  }, {
    pwd
  }).then(data => {
    res.send({
      code: 200,
      success: true,
      msg: '修改密码成功'
    })
  })
})

//生成token
function createToken(tel) {
  //生成token
  let rule = {
    tel
  }
  const token = jwt.sign(rule, 'jwt', {
    expiresIn: 60*30
  })
  return token
}
module.exports = router
